package net.lab1024.smartadmin.module.business.drivecycleTab.controller;

import net.lab1024.smartadmin.common.domain.PageResultDTO;
import net.lab1024.smartadmin.common.controller.BaseController;
import net.lab1024.smartadmin.common.domain.ResponseDTO;
import net.lab1024.smartadmin.common.domain.ValidateList;
import net.lab1024.smartadmin.module.business.drivecycleTab.domain.dto.DrivingCycleTbAddDTO;
import net.lab1024.smartadmin.module.business.drivecycleTab.domain.dto.DrivingCycleTbUpdateDTO;
import net.lab1024.smartadmin.module.business.drivecycleTab.domain.dto.DrivingCycleTbQueryDTO;
import net.lab1024.smartadmin.module.business.drivecycleTab.domain.entity.DrivingCycleTbEntity;
import net.lab1024.smartadmin.module.business.drivecycleTab.domain.vo.DrivingCycleTbVO;
import net.lab1024.smartadmin.module.business.drivecycleTab.domain.vo.DrivingCycleTbExcelVO;
import net.lab1024.smartadmin.module.business.drivecycleTab.service.DrivingCycleTbService;
import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import net.lab1024.smartadmin.module.business.gearboxEfficiency.domain.entity.GearboxEfficiencyEntity;
import net.lab1024.smartadmin.util.SmartRequestTokenUtil;
import org.apache.poi.ss.usermodel.Workbook;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * [ 行驶工况特性曲线table数据 ]
 *
 * @author wuhq
 * @version 1.0
 * @company 汽车传动
 * @copyright (c)  汽车传动Inc. All rights reserved.
 * @date 2022-01-22 11:43:10
 * @since JDK1.8
 */
@RestController
@Api(tags = {"行驶工况特性曲线table数据"})
public class DrivingCycleTbController extends BaseController {

    @Autowired
    private DrivingCycleTbService drivingCycleTbService;

    @ApiOperation(value = "分页查询行驶工况特性曲线table数据",notes = "@author wuhq")
    @PostMapping("/drivingCycleTb/page/query")
    public ResponseDTO<PageResultDTO<DrivingCycleTbVO>> queryByPage(@RequestBody DrivingCycleTbQueryDTO queryDTO) {
        return drivingCycleTbService.queryByPage(queryDTO);
    }

    @ApiOperation(value = "添加行驶工况特性曲线table数据",notes = "@author wuhq")
    @PostMapping("/drivingCycleTb/add")
    public ResponseDTO<String> add(@RequestBody @Validated DrivingCycleTbAddDTO addTO){
        addTO.setCreateUser(SmartRequestTokenUtil.getRequestUserId());
        addTO.setCreateTime(new Date());
        // 保存之前先删除旧的数据
        drivingCycleTbService.delByDrivingCycleId(addTO.getDrivingCycleId());

        return drivingCycleTbService.add(addTO);
    }

    @ApiOperation(value = "根据工况信息id获取数据",notes = "@author wuhq")
    @GetMapping("/drivingCycleTb/getByDrivingCycleId/{drivingCycleId}")
    public ResponseDTO<DrivingCycleTbEntity> getByDrivingCycleId(@PathVariable Long drivingCycleId) {
        return ResponseDTO.succData(drivingCycleTbService.getByDrivingCycleId(drivingCycleId));
    }

    @ApiOperation(value="修改行驶工况特性曲线table数据",notes = "@author wuhq")
    @PostMapping("/drivingCycleTb/update")
    public ResponseDTO<String> update(@RequestBody @Validated DrivingCycleTbUpdateDTO updateDTO){
        return drivingCycleTbService.update(updateDTO);
    }

    @ApiOperation(value="批量删除行驶工况特性曲线table数据",notes = "@author wuhq")
    @PostMapping("/drivingCycleTb/deleteByIds")
    public ResponseDTO<String> delete(@RequestBody @Validated ValidateList<Long> idList) {
        return drivingCycleTbService.deleteByIds(idList);
    }

    @ApiOperation(value = "批量导出", notes = "@author wuhq")
    @PostMapping("/drivingCycleTb/export/batch")
    public void batchExport(@RequestBody @Validated ValidateList<Long> idList, HttpServletResponse response) {
        //查询数据
        List<DrivingCycleTbExcelVO> drivingCycleTbList = drivingCycleTbService.queryBatchExportData(idList);
        //导出操作
        ExportParams ex = new ExportParams("行驶工况特性曲线table数据", "Sheet1");
        Workbook workbook = ExcelExportUtil.exportExcel(ex, DrivingCycleTbExcelVO.class, drivingCycleTbList);
        downloadExcel("行驶工况特性曲线table数据", workbook, response);
    }

    @ApiOperation(value = "导出全部", notes = "@author wuhq")
    @PostMapping("/drivingCycleTb/export/all")
    public void exportAll(@RequestBody @Validated DrivingCycleTbQueryDTO queryDTO, HttpServletResponse response) {
        //查询数据
        List<DrivingCycleTbExcelVO> drivingCycleTbList = drivingCycleTbService.queryAllExportData(queryDTO);
        //导出操作
        ExportParams ex = new ExportParams("行驶工况特性曲线table数据", "Sheet1");
        Workbook workbook = ExcelExportUtil.exportExcel(ex, DrivingCycleTbExcelVO.class, drivingCycleTbList);
        downloadExcel("行驶工况特性曲线table数据", workbook, response);
    }

}
